package org.kramaframework.tag.widget;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import org.kramaframework.panel.TGlobalPanel;



/**
 * This class defined a style of Widget
 * 
 * @author chabane
 * @version 1.0
 */
public class CssWidget {

	/**
	 * CSS attributs of Widget
	 */
	private String cssName;
	private String cssParentName;
	private HashMap cssAttributAndValue;
	private String prefixCss;
	private String suffixCss;
	private String attributsCss;
	private String othersCSS;
	private String parentName;
	private String moreCss;
	
	/**
	 * Constructor
	 * @param parentName
	 * @param cssName
	 */
	public CssWidget(String cssName) {
		cssAttributAndValue = new HashMap();
		this.cssName = cssName;

		prefixCss = cssName;
		attributsCss = " { \n ";
		suffixCss = " \n}\n\n";
		othersCSS = "\n";
		moreCss = "\n";
	}

	/**
	 * Add CSS attribut into HashMap
	 * 
	 * @param name
	 * @param value
	 */
	public void addAttribut(String name, String value) {
		cssAttributAndValue.put(name, value);
	}

	/**
	 * Get css attribut from HashMap
	 * 
	 * @param name
	 * @return css attribut
	 */
	public String getAttribut(String name) {
		return ""+cssAttributAndValue.get(name);
	}

	/**
	 * Remove CSS attribut of HashMap
	 * 
	 * @param name
	 */
	public void removeAttribut(String name) {
		cssAttributAndValue.remove(name);
	}

	/**
	 * Check if CSS attributs provided on paramater is available
	 * 
	 * @param name
	 * @return boolean
	 */
	public boolean isAttributAvailable(String name) {
		return cssAttributAndValue.containsKey(name);
	}

	/**
	 * Get css of this Widget
	 * 
	 * @param name
	 * @return All CSS of Widget
	 */
	public String getCSS() {

		for(Object key : cssAttributAndValue.keySet()) {
			attributsCss += "\n \t"+key+":"+cssAttributAndValue.get(key)+"; ";
		}
		cssAttributAndValue.clear();
		return prefixCss + attributsCss + moreCss + suffixCss + othersCSS;
	}
	
	/**
	 * Set a parent name of CSS class
	 * @param parentName
	 */
	public void setParentName(String parentName){
		prefixCss = "."+parentName + " ."+cssName;
		this.parentName = parentName;
	}
	
	/**
	 * Set a parent name of CSS class
	 * @param parentName
	 */
	public void setParentName(String globalCssName, String parentName, String panelName){
		
		if(globalCssName == null)
			globalCssName = TGlobalPanel.name;
		
		prefixCss = " ."+globalCssName + " ." + parentName + " ."+ panelName + " ."+cssName;
	}
	
	/**
	 * Get a parent name of CSS class
	 * @param parentName
	 */
	public String getParentName(){
		return parentName;
	}

	/**
	 * Add others CSS from widget child
	 * @param othersCSS
	 */
	public void addOthersCSS(String othersCSS) {
		this.othersCSS += othersCSS; 
	}

	/**
	 * Add a custom css class to current widget
	 * @param globalCssName
	 * @param parentName
	 * @param panelName
	 * @param attributesMap
	 */
	public void addCostumCSS(String globalCssName, String parentName,
			String panelName, HashMap attributesMap) {
		
		if(globalCssName == null)
			globalCssName = TGlobalPanel.name;
		String prefix = " ."+globalCssName + " ." + parentName + " ."+ panelName;
		
		String attributes = " { \n ";
		for(Object key : attributesMap.keySet()) {
			attributes += "\n \t"+key+":"+attributesMap.get(key)+"; ";
		}

		this.othersCSS += prefix + attributes + suffixCss;
	}

	/**
	 * Add any css on this widget
	 * @param css
	 */
	public void addCSS(String moreCss) {
		this.moreCss += moreCss;
	}
	
	/**
	 * Get other css from same css class
	 * @return more css added by widget
	 */
	public String getMoreCss() {
		return moreCss;
	}
}
